package com.shixi.shixitraining.shixitraining.ui.tabhome.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.shixi.shixitraining.shixitraining.R;
import com.shixi.shixitraining.shixitraining.base.BaseActivity;
import com.shixi.shixitraining.shixitraining.http.CallbackImple;
import com.shixi.shixitraining.shixitraining.http.HttpUtil;
import com.shixi.shixitraining.shixitraining.ui.tabhome.entity.TrainingSubmitApplyBean;
import com.shixi.shixitraining.shixitraining.ui.tabhome.entity.UpdataBean;
import com.shixi.shixitraining.shixitraining.util.ToastUtil;
import com.shixi.shixitraining.shixitraining.util.UserManager;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;

public class PracticalApplicationActivity extends BaseActivity implements View.OnClickListener {


//    @BindView(R.id.iv_back)
//    ImageView ivBack;
//    @BindView(R.id.tv_name)
//    TextView tvName;
//    @BindView(R.id.edt_name)
//    EditText edtName;
//    @BindView(R.id.tv_group)
//    TextView tvGroup;
//    @BindView(R.id.tv_select)
//    TextView tvSelect;
//    @BindView(R.id.edt_phone)
//    EditText edtPhone;
//    @BindView(R.id.edt_email)
//    EditText edtEmail;
//    @BindView(R.id.no_tv)
//    TextView noTv;
//    @BindView(R.id.yes_tv)
//    TextView yesTv;
//    @BindView(R.id.school_tv)
//    TextView schoolTv;
//    @BindView(R.id.gradute_tv)
//    TextView graduteTv;
//    @BindView(R.id.edt_wechat)
//    EditText edtWechat;
//    @BindView(R.id.edt_age)
//    EditText edtAge;
//    @BindView(R.id.edt_major)
//    EditText edtMajor;
//    @BindView(R.id.edt_grade)
//    EditText edtGrade;
//    @BindView(R.id.edt_address)
//    EditText edtAddress;
//    @BindView(R.id.tv_resume)
//    TextView tvResume;
//    @BindView(R.id.tv_application_letter)
//    TextView tvApplicationLetter;
//    @BindView(R.id.edt_referrer)
//    EditText edtReferrer;
//    @BindView(R.id.practice_confirm)
//    Button practiceConfirm;
    private TextView mSelectTv;
    private TextView mNoTv;
    private TextView mYesTv;
    private boolean isSelect = false;
    private TextView atSchoolTv;
    private TextView gradute;
    private Button practiceConfirm;
    private TextView edtName;
    private TextView edtPhone;
    private TextView edtWechat;
    private TextView edtAge;
    private TextView edtMajor;
    private TextView edtEmail;
    private TextView edtGrade;
    private TextView edtAddress;
    private TextView edtReferrer;
    private TextView mResumeTv;
    private TextView mLettertV;
    private String litterName;
    private String resumeName;
    private String resumeSource;
    private String litterSource;
    private String trainingId;
    private String detailesId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_practical_application);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if (intent==null){
            int training = bundle.getInt("training");
            int details = bundle.getInt("details");
            trainingId = String.valueOf(training);
            detailesId = String.valueOf(details);
        }
        initView();
    }

    private void initView() {
        ImageView mBackImg = (ImageView) findViewById(R.id.iv_back);
        practiceConfirm = (Button) findViewById(R.id.practice_confirm);
        edtName = (TextView) findViewById(R.id.edt_name);
        edtPhone = (TextView) findViewById(R.id.edt_phone);
        edtEmail = (TextView) findViewById(R.id.edt_eamil);
        edtWechat = (TextView) findViewById(R.id.edt_wechat);
        edtAge = (TextView) findViewById(R.id.edt_age);
        edtMajor = (TextView) findViewById(R.id.edt_major);
        edtGrade = (TextView) findViewById(R.id.edt_grade);
        edtAddress = (TextView) findViewById(R.id.edt_address);
        edtReferrer = (TextView) findViewById(R.id.edt_referrer);
        mSelectTv = (TextView) findViewById(R.id.tv_select);
        mNoTv = (TextView) findViewById(R.id.no_tv);
        mYesTv = (TextView) findViewById(R.id.yes_tv);
        atSchoolTv = (TextView) findViewById(R.id.school_tv);
        gradute = (TextView) findViewById(R.id.gradute_tv);
        mResumeTv = (TextView) findViewById(R.id.tv_application_resume);
        mLettertV = (TextView) findViewById(R.id.tv_application_letter);
        atSchoolTv.setOnClickListener(this);
        gradute.setOnClickListener(this);
        mNoTv.setOnClickListener(this);
        mYesTv.setOnClickListener(this);
        mSelectTv.setOnClickListener(this);
        practiceConfirm.setOnClickListener(this);
        mBackImg.setOnClickListener(this);
        mResumeTv.setOnClickListener(this);
        mLettertV.setOnClickListener(this);


        if (TextUtils.isEmpty(edtName.getText().toString())) {
            ToastUtil.showShortToast(PracticalApplicationActivity.this, "名字不能为空");
            return;
        } else if (TextUtils.isEmpty(edtPhone.getText().toString())) {
            ToastUtil.showShortToast(PracticalApplicationActivity.this, "手机号不能为空");
            return;
        } else if (TextUtils.isEmpty(edtEmail.getText().toString())) {
            ToastUtil.showShortToast(PracticalApplicationActivity.this, "邮箱不能为空");
            return;
        } else if (TextUtils.isEmpty(edtWechat.getText().toString())) {
            ToastUtil.showShortToast(PracticalApplicationActivity.this, "微信号不能为空");
            return;
        } else if (TextUtils.isEmpty(edtAge.getText().toString())) {
            ToastUtil.showShortToast(PracticalApplicationActivity.this, "年龄不能为空");
            return;
        } else if (TextUtils.isEmpty(edtMajor.getText().toString())) {
            ToastUtil.showShortToast(PracticalApplicationActivity.this, "专业不能为空");
            return;
        } else if (TextUtils.isEmpty(edtGrade.getText().toString())) {
            ToastUtil.showShortToast(PracticalApplicationActivity.this, "年级不能为空");
            return;
        } else if (TextUtils.isEmpty(edtAddress.getText().toString())) {
            ToastUtil.showShortToast(PracticalApplicationActivity.this, "地址不能为空");
            return;
        } else if (TextUtils.isEmpty(edtReferrer.getText().toString())) {
            ToastUtil.showShortToast(PracticalApplicationActivity.this, "推荐人不能为空");
            return;
        }

    }



    @OnClick({R.id.iv_back, R.id.tv_resume, R.id.tv_application_letter, R.id.practice_confirm})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.practice_confirm:
                submitTrainingApply();
//                startActivity(new Intent(this, ApplyCompleteActivity.class));
                break;
            case R.id.tv_select:
                selectCountry();
                break;
            case R.id.no_tv:
                if (isSelect) {
                    mNoTv.setBackgroundResource(R.drawable.select_no_press);
                    mYesTv.setBackgroundResource(R.drawable.select_press);
                } else {
                    mNoTv.setBackgroundResource(R.drawable.select_press);
                    mYesTv.setBackgroundResource(R.drawable.select_no_press);
                }
                break;
            case R.id.yes_tv:
                if (isSelect) {
                    mYesTv.setBackgroundResource(R.drawable.select_no_press);
                    mNoTv.setBackgroundResource(R.drawable.select_press);
                } else {
                    mYesTv.setBackgroundResource(R.drawable.select_press);
                    mNoTv.setBackgroundResource(R.drawable.select_no_press);
                }
                break;
            case R.id.school_tv:
                if (isSelect) {
                    atSchoolTv.setBackgroundResource(R.drawable.select_no_press);
                    gradute.setBackgroundResource(R.drawable.select_press);
                } else {
                    atSchoolTv.setBackgroundResource(R.drawable.select_press);
                    gradute.setBackgroundResource(R.drawable.select_no_press);
                }
                break;
            case R.id.gradute_tv:
                if (isSelect) {
                    gradute.setBackgroundResource(R.drawable.select_no_press);
                    atSchoolTv.setBackgroundResource(R.drawable.select_press);
                } else {
                    gradute.setBackgroundResource(R.drawable.select_press);
                    atSchoolTv.setBackgroundResource(R.drawable.select_no_press);
                }
                break;
            case R.id.tv_application_resume:
                updateResume();
                break;
            case R.id.tv_application_letter:
                updateLetter();
                break;
        }
    }

    private void updateLetter() {
        String file="";
        Map<String,Object> map=new HashMap<>();
        map.put("file",file);
        HttpUtil.getInstance().getApiInterface().updataFile(map).enqueue(new CallbackImple<UpdataBean>() {

            @Override
            public void onError(Call<UpdataBean> call, Throwable t) {
                ToastUtil.showShortToast(PracticalApplicationActivity.this,t.getMessage());
            }

            @Override
            public void onSuccess(Call<UpdataBean> call, UpdataBean response) {
            if (response.getStatus()==200){
                if (response.getData()!=null){
                     UpdataBean.DataBean data = response.getData();
                     litterName = data.getFilename();
                     litterSource = data.getSource();

                }else {
                     ToastUtil.showShortToast(PracticalApplicationActivity.this,response.getMsg());

                }
          }else {
                ToastUtil.showShortToast(PracticalApplicationActivity.this,"数据为空");
        }
            }
        });


    }

    private void updateResume() {
//        String file="";
//        Map<String,String> map=new HashMap<>();
//        map.put("file",file);

        File file=new File(Environment.getExternalStorageDirectory(),"jeff的简历.doc");


        HttpUtil.getInstance().getApiInterface().upFile(file).enqueue(new CallbackImple<UpdataBean>() {

            @Override
            public void onError(Call<UpdataBean> call, Throwable t) {
                ToastUtil.showShortToast(PracticalApplicationActivity.this,t.getMessage());
            }

            @Override
            public void onSuccess(Call<UpdataBean> call, UpdataBean response) {
                if (response.getStatus()==200){

                    if (response.getData()!=null){
                        UpdataBean.DataBean data = response.getData();
                        resumeName = data.getFilename();
                        resumeSource = data.getSource();

                    }else {
                        ToastUtil.showShortToast(PracticalApplicationActivity.this,response.getMsg());
                    }

                }else {
                 ToastUtil.showShortToast(PracticalApplicationActivity.this,"数据加载失败");
                }

            }
        });

    }

    private void submitTrainingApply() {

        String token = UserManager.getInstance().getToken();
        String userId = UserManager.getInstance().getUserId();
        Map<String,Object> map=new HashMap<>();
        map.put("token",token);
        map.put("userId",userId);
        map.put("training",trainingId);
        map.put("details",detailesId);
        map.put("telephone",edtPhone);
        map.put("email",edtEmail);
        map.put("wechat",edtWechat);
        map.put("fullname",edtName);
        map.put("age",edtAge);
        map.put("graduation",edtMajor);
        map.put("school","");
        map.put("discipline","");
        map.put("grade",edtGrade);
        map.put("us_visa","");
        map.put("country","");
        map.put("address",edtAddress);
        map.put("resume_path",resumeSource);
        map.put("resume_name",resumeName);
        map.put("letter_path",litterSource);
        map.put("letter_name",litterName);
        map.put("promoter",edtReferrer);
        HttpUtil.getInstance().getApiInterface().trainingSubmitApply(map).enqueue(new CallbackImple<TrainingSubmitApplyBean>() {
            @Override
            public void onError(Call<TrainingSubmitApplyBean> call, Throwable t) {

                ToastUtil.showShortToast(PracticalApplicationActivity.this,t.getMessage());
            }

            @Override
            public void onSuccess(Call<TrainingSubmitApplyBean> call, TrainingSubmitApplyBean response) {

                if (response.getStatus()==200){
                    if (response.getData()!=null){
                        TrainingSubmitApplyBean.DataBean data = response.getData();
                        String number = data.getNumber();
                        ToastUtil.showShortToast(PracticalApplicationActivity.this,number);

                    }else {
                        ToastUtil.showShortToast(PracticalApplicationActivity.this,response.getMsg());
                    }

                }else {
                    ToastUtil.showShortToast(PracticalApplicationActivity.this,"数据加载失败");
                }
            }
        });


    }


    private void selectCountry() {
        String[] stringArray = mSelectTv.getResources().getStringArray(R.array.country_arrays);
        final List<String> countryList = new ArrayList<>();
        for (int i = 0; i < stringArray.length; i++) {
            countryList.add(stringArray[i]);

        }
        OptionsPickerView optionsPickerView = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                mSelectTv.setText(countryList.get(options1));
                String country = countryList.get(options1);
                String state = countryList.get(options2);
                String city = countryList.get(options3);
            }
        }).build();

        optionsPickerView.setPicker(countryList);
        optionsPickerView.show();

    }

    private void applyAerDialog() {

        final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setIcon(R.drawable.icon_complete)
                .setTitle("恭喜您，职位申请成功")
                .setMessage("申请序号：SX000001")
                .setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        dialog.show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
